package com.panda.pandawash.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.panda.pandawash.R;

import java.io.InputStream;

public class MarketFragment extends Fragment implements AdapterView.OnItemClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private String mParam1;

    private OnFragmentInteractionListener mListener;

    private int[] productImg = {R.drawable.ic_device1,R.drawable.ic_device2,R.drawable.ic_device3,R.drawable.ic_device4};
    private int[] productTitle = {R.string.product1_title,R.string.product2_title,R.string.product3_title,R.string.product4_title};
    private int[] productMoney = {R.string.product1_money,R.string.product2_money,R.string.product3_money,R.string.product4_money};
    private String url;
    private Intent myIntent;


    public MarketFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MarketFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MarketFragment newInstance() {
        MarketFragment fragment = new MarketFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_market, container, false);
        GridView gridView = (GridView) view.findViewById(R.id.product_gridView);
        gridView.setAdapter(new MyAdapter(getContext(),productImg,productTitle,productMoney));
        gridView.setOnItemClickListener(this);
        return view;
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:
                url = "https://h5.m.taobao.com/awp/core/detail.htm?id=40495990380"; // web address
                myIntent = new Intent(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse(url));
                startActivity(myIntent);
                break;
            case 1:
                url = "https://h5.m.taobao.com/awp/core/detail.htm?id=525612704822"; // web address
                myIntent = new Intent(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse(url));
                startActivity(myIntent);
                break;
            case 2:
                url = "https://h5.m.taobao.com/awp/core/detail.htm?id=40431574032"; // web address
                myIntent = new Intent(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse(url));
                startActivity(myIntent);
                break;
            case 3:
                url = "http://c.b1wt.com/h.ejvLG6?cv=ycPln9Do7C&sm=e8815b";
                myIntent = new Intent(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse(url));
                startActivity(myIntent);

                break;

            default:
                break;

        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     *@param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId){
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is,null,opt);
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     *@param resId
     * @return
     */
    public static String changeToString(Context context, int resId){
        return context.getResources().getString(resId).toString();
    }

    private static class MyAdapter extends BaseAdapter {
        private LayoutInflater layoutInflater;
        private int[] images;
        private int[] text1;
        private int[] text2;
        private Context mContext;
        public MyAdapter(Context context,int[] images,int[] text1, int[] text2){
            this.mContext = context;
            this.images = images;
            this.text1 = text1;
            this.text2 = text2;
            layoutInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return images[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = layoutInflater.inflate(R.layout.product_item,null);
            ImageView productImg = (ImageView) v.findViewById(R.id.product_img);
            TextView productTitle = (TextView) v.findViewById(R.id.product_title);
            TextView productMoney = (TextView) v.findViewById(R.id.product_money);
            productImg.setImageBitmap(readBitMap(mContext,images[position]));
            productTitle.setText(changeToString(mContext,text1[position]));
            productMoney.setText(changeToString(mContext,text2[position]));
            return v;
        }
    }
}
